package com.zys.el.user.client;


import com.zys.el.time.entity.AbsorbedDeal;
import com.zys.el.time.entity.Deal;
import com.zys.el.time.vojo.Result;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@FeignClient(name = "time-service", fallbackFactory = /*AbsorbedDealClient.AbsorbedDealClientFallback.class*/ AbsorbedDealClient.AbsorbedDealClientFallbackFactory.class)
@RequestMapping(value = "/absorbed_deal")
public interface AbsorbedDealClient {

    @GetMapping("/{deal_id}")
    public List<AbsorbedDeal> getAbDealByDealId(@PathVariable("deal_id") String dealId);
    @GetMapping("/user/{user_id}")
    public List<AbsorbedDeal> getAbDealByUserId(@PathVariable("user_id") String userId);

//    @Component
//    @RequestMapping("/time-fallback")
//    class AbsorbedDealClientFallback implements AbsorbedDealClient {
//        private static final Logger LOGGER = LoggerFactory.getLogger(AbsorbedDealClient.AbsorbedDealClientFallback.class);
//
//        @Override
//        public List<AbsorbedDeal> getAbDealByDealId(@PathVariable("deal_id") String dealId) {
//            System.out.println("fall:" + dealId);
//            LOGGER.error("异常出现，进入fallback方法");
//            return null;
//        }
//
//        @Override
//        public List<AbsorbedDeal> getAbDealByUserId(String userId) {
//            System.out.println("fall:" + userId);
//            LOGGER.error("异常出现，进入fallback方法");
//            return null;
//        }
//    }

    @Component
    @RequestMapping("/absorbed-deal-fallback-factory")
    class AbsorbedDealClientFallbackFactory implements FallbackFactory<AbsorbedDealClient> {

        Logger LOGGER = LoggerFactory.getLogger(AbsorbedDealClient.AbsorbedDealClientFallbackFactory.class);

        @Override
        public AbsorbedDealClient create(Throwable throwable) {
            throwable.printStackTrace();
            LOGGER.error(throwable.getMessage());
            return new AbsorbedDealClient() {
                @Override
                public List<AbsorbedDeal> getAbDealByDealId(String dealId) {
                    LOGGER.error("异常出现，进入fallback方法");
                    return null;
                }

                @Override
                public List<AbsorbedDeal> getAbDealByUserId(String userId) {
                    LOGGER.error("异常出现，进入fallback方法");
                    return null;
                }
            };
        }
    }


}
